//
//  ThemeView.swift
//  KaMengApplication
//
//  Created by DUONIU_MAC on 2018/8/31.
//  Copyright © 2018年 DUONIU_MAC. All rights reserved.
//

import UIKit

@objc protocol ThemeViewDelegate {
    @objc optional func selectCardType(title:String) //选择卡片类型
}

class ThemeView: UIView {
    
    weak var themeDelegate : ThemeViewDelegate?
    
    fileprivate let cellId = "themeCell"
    
    fileprivate let dataArray = [shoppingCard,studentCard,sportCard,businessCard,standardCard,ownerCard]
    fileprivate let images = [#imageLiteral(resourceName: "gwk"),UIImage.init(named: "xsk"),#imageLiteral(resourceName: "ydk"),#imageLiteral(resourceName: "slk"),#imageLiteral(resourceName: "bzk"),#imageLiteral(resourceName: "czk")]

    init() {
        super.init(frame: .zero)
        
        self.backgroundColor = UIColor.white
        
        let layout = UICollectionViewFlowLayout()
        layout.minimumLineSpacing = 0
        layout.minimumInteritemSpacing = 0
        layout.scrollDirection = .vertical
        layout.itemSize = CGSize.init(width: ScreenW/4, height: RATIO_H(maxNum: 90))
        
        let collectionView = UICollectionView.init(frame: .zero, collectionViewLayout: layout)
        collectionView.isScrollEnabled = false
        collectionView.backgroundColor = UIColor.white
        collectionView.delegate = self
        collectionView.dataSource = self
        collectionView.register(CardHeadCell.self, forCellWithReuseIdentifier: cellId)
        self.addSubview(collectionView)
        
        collectionView.snp.makeConstraints { (make) in
            make.top.left.right.equalToSuperview()
            make.height.equalTo(RATIO_H(maxNum: 160))
        }
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}

extension ThemeView : UICollectionViewDelegate,UICollectionViewDataSource {
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        return dataArray.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath) as? CardHeadCell
        cell?.titleLabel.text = dataArray[indexPath.row]
        cell?.headImageView.image = images[indexPath.row]
        
        if indexPath.row == 3 {
            let path = Bundle.main.path(forResource: "ticket", ofType: "gif")
            let data = NSData.init(contentsOfFile: path!)
            let image = UIImage.sd_animatedGIF(with: data! as Data)
            
            let imageView = UIImageView()
            imageView.image = image
            imageView.contentMode = .scaleAspectFit
            cell?.contentView.addSubview(imageView)
            
            imageView.snp.makeConstraints { (make) in
                make.left.equalTo(cell!.headImageView.snp.right).offset(-5)
                make.top.equalTo((cell?.headImageView)!).offset(RATIO_H(maxNum: 0))
                make.width.equalTo(25)
                make.height.equalTo(12)
            }
            
        }
        
        return cell!
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        collectionView.deselectItem(at: indexPath, animated: true)
        
        self.themeDelegate?.selectCardType!(title: dataArray[indexPath.row])
    }
    
}
